import { Component, ElementRef, ViewChild, OnInit, Inject } from '@angular/core';
import { DataSource } from '@angular/cdk/collections';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/observable/fromEvent';
import { MdPaginator, MdSnackBar, MD_DIALOG_DATA } from "@angular/material";
import { ApiServiceService } from '../api.service'
import { MdDialog, MdDialogRef } from '@angular/material';
import { ConstantService } from "../service/constant.service";
import { SortablejsModule } from "angular-sortablejs/dist/index";
import {
  CommonService
} from "../service/common.service";

import { DialogService } from '../service/dialog.service';

@Component({
  selector: 'app-paly-request',
  templateUrl: './paly-request.component.html',
  styleUrls: ['./paly-request.component.css'],
  providers: [ConstantService]
})
export class PalyRequestComponent implements OnInit {
  view
  playRequest: any = {};
  supportMusicFileType = [];
  eventLevels = [
    "未知级别",
    "1级（特别重大/红色预警/Red）",
    "2级（重大/橙色预警/ Orange）",
    "3级（较大/黄色预警/ Yellow）",
    "4级（一般/蓝色预警/ Blue）"
  ]
  constructor(private apiService: ApiServiceService,
    public snackBar: MdSnackBar,
    public dialog: MdDialog,
    private dialogService: DialogService,
    private constantService: ConstantService,
    private commonService: CommonService,
  ) {
    this.supportMusicFileType = this.constantService.supportMusicFileType;
  }

  ngOnInit() {
    this.playRequest.beginHour = this.commonService.dateFormat(new Date(), "hh");
    this.playRequest.beginMin = Number(this.commonService.dateFormat(new Date(), "mm")) + 2;
    this.playRequest.beginSec = this.commonService.dateFormat(new Date(), "ss");
    this.playRequest.endHour = this.commonService.dateFormat(new Date(), "hh");
    this.playRequest.endMin = Number(this.commonService.dateFormat(new Date(), "mm")) + 5;
    this.playRequest.endSec = this.commonService.dateFormat(new Date(), "ss");
    this.playRequest.beginDate = new Date()
    this.playRequest.endDate = new Date()
    this.playRequest.AreaCode = "341523000000"
    this.playRequest.EventType = "00000"
    this.playRequest.Severity = 0
  }



  fileChange(e) {
    this.playRequest.file = e.target.files[0];
  }

  getStartTime() {
    let a = new Date(this.playRequest.beginDate);
    a.setHours(this.playRequest.beginHour);
    a.setMinutes(this.playRequest.beginMin);
    a.setSeconds(this.playRequest.beginSec);
    return a;
  }

  getEndTime() {
    let a = new Date(this.playRequest.endDate);
    a.setHours(this.playRequest.endHour);
    a.setMinutes(this.playRequest.endMin);
    a.setSeconds(this.playRequest.endSec);
    return a;
  }

  sendplayRequest() {
    if (this.playRequest.file) {
      let nameArray = this.playRequest.file.name.split('.');
      let flag = false;
      this.supportMusicFileType.forEach((item, index) => {
        if (item.toLowerCase() == nameArray[nameArray.length - 1].toLowerCase()) {
          flag = true;
        }
      })
      if (!flag) {
        return this.openSnackBar("当前音乐类型不支持", "Ok")
      }
    }
    if (this.playRequest.MsgTitle && this.playRequest.MsgDesc && this.playRequest.AreaCode && this.playRequest.EventType && (this.playRequest.Severity!== undefined)) {
      let formData: FormData = new FormData();
      if (this.playRequest.file) {
        formData.append("file", this.playRequest.file)
      }
      formData.append("MsgTitle", this.playRequest.MsgTitle)
      formData.append("MsgDesc", this.playRequest.MsgDesc)
      formData.append("EventType", this.playRequest.EventType)
      formData.append("Severity", this.playRequest.Severity)
      formData.append("AreaCode", this.playRequest.AreaCode)
      formData.append("StartTime", this.getStartTime().toString())
      formData.append("EndTime", this.getEndTime().toString())
      this.apiService.PlayRequest(formData).subscribe(res => {
        if (res.success) {
          this.openSnackBar("播发成功", "Ok")
        }
      })
    } else {
      return this.openSnackBar("有必填项未填", "Ok")
    }
    console.log(this.getStartTime())
  }

  openSnackBar(message: string, action: string) {
    this.snackBar.open(message, action, {
      duration: 3000,
    });
  }

  openLoadingDialog() {
    return this.dialog.open(LoadingDialog);
  }
}
@Component({
  selector: 'loading',
  templateUrl: './loading.html',
})
export class LoadingDialog {
}
